1731D - Valiant's New Map - CodeForces Solution


binary search brute force data structures dp two pointers *1700

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#define ll long long
#define lld long double
#define ff first
#define ss second
#define pb push_back
#define vr(v) v.begin(),v.end()
#define rv(v) v.rbegin(),v.rend()
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Davit cout.tie(NULL);

using namespace std;
const int INF = 1e9;
// printf("%.9f\n", x);
//cout << fixed<<setprecision(n);
//    freopen("x.in", "r", stdin);
//    freopen("x.out", "w", stdout);

//mt19937 mt(time(nullptr));
//__builtin_clz(x): the number of zeros at the beginning of the number
//__builtin_ctz(x): the number of zeros at the end of the number
//__builtin_popcount(x): the number of 1s

int n, m;


bool ok(int mid, vector<vector<int>> v) {
    vector<vector<int>> pref(n + 1, vector<int>(m + 1));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (v[i][j] >= mid) v[i][j] = 1;
            else v[i][j] = 0;
        }
    }
//    cerr << "1\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            pref[i + 1][j + 1] = pref[i][j + 1] + pref[i + 1][j] - pref[i][j] + v[i][j];
        }
    }
//    cerr << "2\n";
    bool isok = 0;
    for (int i = mid; i <= n; i++) {
        for (int j = mid; j <= m; j++) {
            int cur = pref[i][j] - pref[i][j - mid] - pref[i - mid][j] + pref[i - mid][j - mid];
            if (cur == mid * mid)return 1;
        }
    }

    return 0;
}


void solve() {
    cin >> n >> m;
    vector<vector<int>> maze(n, vector<int>(m));
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++)cin >> maze[i][j];

    int l = 0, r = n + 1;
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (ok(mid, maze))l = mid;
        else r = mid;
    }
    cout << l << "\n";
}


int main() {
    int t = 1;
    cin >> t;
    while (t--)solve();
}


Comments

Submit
0 Comments
More Questions

1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture